<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar left-arrow title="编辑资料" @click-left="toMy"></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
      <div class="crop-box" v-show="isShowcrop">
        <vueCropper
          ref="cropper"
          fixed
          autoCropWidth="120"
          autoCropHeight="120"
          autoCrop
          :img="img"
        ></vueCropper>
        <!-- 两个按钮 -->

        <van-button class="confirm-btn" type="primary" @click="submitPhoto"
          >确定</van-button
        >
        <van-button class="cancel-btn" type="info" @click="isShowcrop = false"
          >取消</van-button
        >
      </div>
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <!-- 名称 -->
      <van-cell is-link title="名称" :value="userInfo.name" @click="tanTips" />
      <!-- 修改名称弹框 -->
      <van-dialog
        v-model="show"
        title="修改名称"
        @confirm="saveName"
        show-cancel-button
      >
        <!-- <img src="https://img01.yzcdn.cn/vant/apple-3.jpg" /> -->
        <van-field v-model="value" placeholder="请输入用户名" ref="nameFiled" />
      </van-dialog>
      <!-- 性别 -->
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender === 0 ? '男' : '女'"
        @click="isShow = true"
      />
      <!-- 修改性别弹框 -->
      <van-popup v-model="isShow" position="bottom" :style="{ height: '30%' }">
        <van-nav-bar
          title="修改性别"
          left-text="取消"
          @click-left="isShow = false"
        />
        <van-cell title="男" is-link size="large" @click="genderShow(0)" />
        <van-cell title="女" is-link size="large" @click="genderShow(1)" />
      </van-popup>
      <!-- 生日 -->
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="showBirthdaypop"
      />
      <!-- 修改生日弹框 -->
      <van-popup position="bottom" v-model="showBirthday">
        <van-datetime-picker
          v-model="birthday"
          @confirm="confirmDate"
          @cancel="cancelDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
// 要使用辅助函数mapState,需要从vuex中导入
import { mapState } from 'vuex'
import { editUserInfo, editUploadFile } from '../../api/edit'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      show: false,
      isShow: false,
      showBirthday: false,
      img: '',
      isShowcrop: false,
      value: '',
      minDate: new Date(2000, 0, 1),
      maxDate: new Date(2025, 10, 1),
      birthday: new Date(2021, 0, 17)
    }
  },
  components: {
    VueCropper
  },
  methods: {
    saveName () {
      editUserInfo({ name: this.value }).then(data => {
        // console.log(data)
        // console.log({ ...this.userInfo, name: 'jack' })
        // 更改本地数据 vuex
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          // 使用最新的name覆盖userInfo里面的name
          name: data.data.data.name
        })
      })
    },
    genderShow (gender) {
      editUserInfo({ gender }).then(data => {
        console.log(data)
        // 修改本地数据 vuex
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender: data.data.data.gender
        })
        this.isShow = false
      })
    },
    showBirthdaypop () {
      this.showBirthday = true
      this.birthday = new Date(this.userInfo.birthday)
    },
    confirmDate () {
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(data => {
        console.log(data)
        this.$toast.success('更新成功！')
        // 更新本地数据
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: data.data.data.birthday
        })
        this.showBirthday = false
      })
    },
    cancelDate () {
      this.showBirthday = false
    },
    submitPhoto () {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(Blob => {
        console.log(Blob) // 裁切好的图片数据
        const fd = new FormData()
        fd.append('photo', Blob)
        editUploadFile(fd).then(data => {
          console.log(data)
          this.$toast.success('更新成功！')
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: data.data.data.photo
          })
        })
        this.isShowcrop = false
      })
    },
    afterRead (file) {
      console.log(file)
      this.isShowcrop = true
      this.img = file.content
    },
    toMy () {
      this.$router.push({ path: '/my' })
    },
    tanTips () {
      this.show = true
      this.value = this.userInfo.name
      // console.log(this,$refs.nameFiled)//undefined
      // 在获取dom元素时，此时this.show=true还没渲染出来，所以要执行$nextTick()
      this.$nextTick(() => {
        this.$refs.nameFiled.focus()
      })
    }
  },
  computed: mapState(['userInfo'])
  /* computed: {
    userInfo () {
      return this.$store.state.userInfo
    }
  } */
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分 navbar这部分样式放到了style-base.less
  /* .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  } */
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    position: relative;
    .van-image {
      width: 120px;
      height: 120px;
    }
    .van-uploader {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      width: 120px;
      height: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
  .van-dialog__content {
    padding: 10px;
  }
  .van-field {
    border: 1px solid #ccc;
  }
  .van-nav-bar__content {
    background-color: #3196fa;
    .van-nav-bar__text {
      color: white;
    }
  }
  .crop-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999999;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
